<template>
  <div class="form-designer">
    <el-container class="designer-container">
      <!-- 左侧面板 -->
      <el-aside width="320px" class="left-panel">
        <el-tabs type="border-card" class="panel-tabs">
          <el-tab-pane label="基础组件">
            <ComponentPanel />
          </el-tab-pane>
          <el-tab-pane label="表单模板">
            <TemplatePanel @template-select="handleTemplateSelect" />
          </el-tab-pane>
        </el-tabs>
      </el-aside>

      <!-- 中间设计画布 -->
      <el-main class="design-canvas">
        <FormCanvas
          :form-items="formItems"
          :selected-item="selectedItem"
          @select="handleSelect"
          @update:form-items="handleFormItemsUpdate"
        />
      </el-main>

      <!-- 右侧属性面板 -->
      <el-aside width="320px" class="property-panel">
        <PropertyPanel
          v-if="selectedItem"
          :selected-item="selectedItem"
          @update:selected-item="handlePropertyUpdate"
        />
        <div v-else class="empty-tip">
          <el-icon><InfoFilled /></el-icon>
          <p>请选择表单元素进行配置</p>
        </div>
      </el-aside>
    </el-container>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
import { InfoFilled } from '@element-plus/icons-vue'
import ComponentPanel from './ComponentPanel.vue'
import FormCanvas from './FormCanvas.vue'
import PropertyPanel from './PropertyPanel.vue'
import TemplatePanel from './TemplatePanel.vue'

export default {
  name: 'FormDesigner',
  components: {
    ComponentPanel,
    FormCanvas,
    PropertyPanel,
    TemplatePanel,
    InfoFilled,
  },
  setup() {
    const formItems = ref([])
    const selectedItem = ref(null)

    const handleSelect = (item) => {
      selectedItem.value = item
    }

    const handleFormItemsUpdate = (items) => {
      formItems.value = items
    }

    const handlePropertyUpdate = (updatedItem) => {
      const index = formItems.value.findIndex((item) => item.id === updatedItem.id)
      if (index !== -1) {
        formItems.value[index] = { ...updatedItem }
      }
    }

    // 新增：处理模板选择
    const handleTemplateSelect = (templateItems) => {
      // 为模板中的每个元素生成唯一ID和model
      const itemsWithIds = templateItems.map((item) => ({
        ...JSON.parse(JSON.stringify(item)),
        id: generateId(),
        model: item.model || `field_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
      }))

      formItems.value = itemsWithIds
      selectedItem.value = null
    }

    const generateId = () => {
      return `form_item_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
    }

    // 获取表单配置（用于保存）
    const getFormConfig = () => {
      return {
        items: formItems.value,
        config: {
          labelWidth: '100px',
          labelPosition: 'right',
        },
      }
    }

    return {
      formItems,
      selectedItem,
      handleSelect,
      handleFormItemsUpdate,
      handlePropertyUpdate,
      handleTemplateSelect,
      getFormConfig,
    }
  },
}
</script>

<style scoped>
.form-designer {
  height: 100%;
}

.designer-container {
  height: 100%;
}

.left-panel {
  background: #f5f7fa;
  border-right: 1px solid #e4e7ed;
}

.panel-tabs {
  height: 100%;
  background: transparent;
}

.panel-tabs :deep(.el-tabs__content) {
  padding: 0;
  height: calc(100% - 40px);
  overflow: auto;
}

.design-canvas {
  background: #f0f2f5;
  padding: 20px;
  overflow: auto;
}

.property-panel {
  background: #f5f7fa;
  border-left: 1px solid #e4e7ed;
  padding: 16px;
}

.empty-tip {
  text-align: center;
  color: #909399;
  padding: 40px 20px;
}

.empty-tip .el-icon {
  font-size: 48px;
  margin-bottom: 16px;
}
</style>
